<?php
/**
* Class and Function List:
* Function list:
* - datefr()
* - datetimefr()
* - DMY_HMS_Fr()
* - jourDayMon()
* - dayMon()
* - formatJour()
* - getDay()
* - getMon()
* - getYear()
* - formatMon()
* - formatTime()
* - formatDate()
* - concat()
* - defineVar()
* - getVar()
* - linkRetour()
* - text()
* - textHTML()
* - error()
* - annonce()
* - htmlencode()
* - htmladdslashes()
* - htmlencode_upper()
* - htmldecode()
* - flash_url_encode()
* - displayTarea()
* - quickform_encode()
* - format_price()
* - format_number()
* - optionInHurry()
* - fullDay()
* - redirect()
* - getNameOfDOStatus()
* - getIconOfDOStatus()
* - buildImagePath()
* - imageHTML()
* - round()
* - DayName()
* - formatURL()
* - encode()
*/
class HTMLOutput
{
    var $variables = array();
    function datefr($date)
    {
        //$date = "yyyy-mm-dd"
        $pos = strpos($date, '-');
        if ($pos === false) return $date;
        if ($date == "0000-00-00") return "";
        $ymd = substr($date, 0, 10);
        list($yyyy, $mm, $dd) = explode("-", $ymd);
        return $dd . "/" . $mm . "/" . $yyyy;

    }

    function datemonthfr($date)
    {
        //$date = "yyyy-mm-dd"
        $pos = strpos($date, '-');
        if ($pos === false) return $date;
        if ($date == "0000-00-00") return "";
        $ymd = substr($date, 0, 10);
        list($yyyy, $mm, $dd) = explode("-", $ymd);
        return $dd . "/" . $mm;

    }

    function datedFY($date)
    {
    	$pos = strpos($date, '-');
        if ($pos === false) return $date;
        if ($date == "0000-00-00") return "";

        return $this->getDay($date) . " " . $this->formatMon($date) . " " . $this->getYear($date);
    }

    function datetimefr($date_time)
    {
        //$date = "yyyy-mm-dd hh:mm:ss"
        $pos = strpos($date_time, '-');
        if ($pos === false) return $date_time;
        if ($date_time == "0000-00-00 00:00:00") return "";
        $ymd = substr($date_time, 0, 10);
        list($yyyy, $mm, $dd) = explode("-", $ymd);
        $hm = substr($date_time, 11, 5);
        return $dd . "/" . $mm . "/" . $yyyy . "&nbsp;-&nbsp;" . $hm;

    }
    function DMY_HMS_Fr($date_time)
    {
        //$date = "yyyy-mm-dd hh:mm:ss"
        $pos = strpos($date_time, '-');
        if ($pos === false) return $date_time;
        if ($date_time == "0000-00-00 00:00:00") return "";
        $ymd = substr($date_time, 0, 10);
        list($yyyy, $mm, $dd) = explode("-", $ymd);
        $hms = substr($date_time, 11, 8);
        return $dd . "/" . $mm . "/" . $yyyy . "&nbsp;-&nbsp;" . $hms;

    }
    function jourDayMon($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        return $this->formatJour($date) . " " . $this->getDay($date) . " " . $this->formatMon($date);

    }
    function dayDateMonthYear($date)
    {
		if (!isset($date) || $date == '')
		{
			return '';
		}
		return $this->formatJour($date) . " " . $this->getDay($date) . " " . $this->formatMon($date) . " " . $this->getYear($date);

    }

    function dayMon($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        return $this->getDay($date) . " " . $this->formatMon($date);

    }
    // Afficher le jour de la semaine
    function formatJour($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        $jour["Monday"] = "Lundi";
        $jour["Tuesday"] = "Mardi";
        $jour["Wednesday"] = "Mercredi";
        $jour["Thursday"] = "Jeudi";
        $jour["Friday"] = "Vendredi";
        $jour["Saturday"] = "Samedi";
        $jour["Sunday"] = "Dimanche";
        return $jour[date('l', strtotime($date)) ];

    }
    function getDay($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        return intval(date('d', strtotime($date)));

    }
    function getMon($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        return intval(date('m', strtotime($date)));

    }
    function getYear($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        return intval(date('Y', strtotime($date)));

    }
    function formatMon($date)
    {
        if (!isset($date) || $date == '')
        {
            return '';

        }
        $months = array(
            "",
            "Janvier",
            "F&eacute;vrier",
            "Mars",
            "Avril",
            "Mai",
            "Juin",
            "Juillet",
            "Ao&ucirc;t",
            "Septembre",
            "Octobre",
            "Novembre",
            "D&eacute;cembre"
        );
        return $months[intval(date('m', strtotime($date))) ];

    }
    // Afficher le jour de la semaine
    function formatTime($date)
    {
        if (!isset($date) || $date == '')
        {
            return '00h 00';

        }
        return date('H', strtotime($date)) . "h" . date('i', strtotime($date));

    }
    function formatDate($date)
    {
        if (!isset($date) || $date == '')
        {
            return '00/00/00';

        }
        return date('d/m/Y', $date);

    }
    function concat($str1, $str2)
    {
        return $str1 . $str2;

    }
    function defineVar($var_name, $value)
    {
        $this->variables[$var_name] = $value;

    }
    function getVar($var_name)
    {
        if (isset($this->variables[$var_name]))
        {
            return $this->variables[$var_name];

        }
        else
        {
            return false;

        }

    }
    function linkRetour($name_obj)
    {
        return "<a href=\"" . $_SESSION[$name_obj . "_link_retour"] . "\">" . _LB_BACK . "</a>";

    }

    function text_addcslashes($str)
    {
    	return addcslashes($str);
    }
    
    function text($str, $encoding = null)
    {
    	if (isset($encoding))
			return $str = stripcslashes(nl2br(htmlentities($str, ENT_QUOTES, $encoding)));
		else
		    return $str = stripcslashes(nl2br(htmlentities($str, ENT_QUOTES, 'UTF-8')));
    }

    function addslashes($str, $encoding = null)
    {
    	if (isset($encoding))
			return $str = nl2br(htmlentities(str_replace("'","\'",$str), ENT_QUOTES, $encoding));
		else
		    return $str = nl2br(htmlentities(str_replace("'","\'",$str), ENT_QUOTES, 'UTF-8'));
    }

    function text_upper($str, $encoding = null)
    {
		if (isset($encoding))
        	return $str = stripcslashes(nl2br(htmlentities(strtoupper($str), ENT_QUOTES, $encoding)));
		else
			return $str = stripcslashes(nl2br(htmlentities(strtoupper($str), ENT_QUOTES, 'UTF-8')));
    }

    function addslashes_upper($str, $encoding = null)
    {
		if (isset($encoding))
        	return $str = nl2br(htmlentities(strtoupper($str), ENT_QUOTES, $encoding));
		else
			return $str = nl2br(htmlentities(strtoupper($str), ENT_QUOTES, 'UTF-8'));
    }


    function textHTML($str)
    {
        //echo $str;
        //$str = Utils::convertEncoding($str);
        return $str = addslashes($str);
        //return  $str = nl2br(htmlentities($str));


    }
    function error($error)
    {
        $str = "";
        if (is_array($error))
        {
            foreach($error as $err)
            {
                $str.= "<tr>
					<td class=\"message_erreur\">$err</td>
				</tr>";

            }

        }
        else
        {
            $str.= "<tr>
					<td class=\"message_erreur\">$error</td>
				</tr>";

        }
        return $str;

    }
    function annonce($annonce)
    {
        $str = "";
        if (is_array($annonce))
        {
            foreach($annonce as $ann)
            {
                $str.= "<tr>
					<td class=\"message_annonce\">$ann</td>
				</tr>";

            }

        }
        else
        {
            $str.= "<tr>
					<td class=\"message_annonce\">$annonce</td>
				</tr>";

        }
        return $str;

    }

    function htmlencode($str,$encode = null )     {
    	if (isset($encoding))
			return $str = stripcslashes(htmlentities($str, ENT_QUOTES, $encoding));
		else
		    return $str = stripcslashes(htmlentities($str, ENT_QUOTES, 'UTF-8'));
    }

    function htmlencode_upper($str)
    {
        //$str = mb_convert_encoding($str,"UTF-8","auto,ISO-8859-1,utf8_general_ci");
            	if (isset($encoding))
			return $str = stripcslashes(htmlentities(strtoupper($str), ENT_QUOTES, $encoding));
		else
		    return $str = stripcslashes(htmlentities(strtoupper($str), ENT_QUOTES, 'UTF-8'));

    }

    function htmladdslashes($str)
    {
        return addslashes($str);

    }

    function htmldecode($str, $charset = 'UTF-8')
    {
        return html_entity_decode($str, ENT_QUOTES, $charset);

    }
    function flash_url_encode($str)
    {
        return urlencode(utf8_encode($str));

    }
    function displayTarea($text)
    {
        $text = $this->htmlencode($text);
        $text = str_replace(array(
            "",
            "& "
        ) , array(
            "&euro;",
            "&amp; "
        ) , $text);
        $text = nl2br($text);
        return $text;

    }
    function quickform_encode($str)
    {
        $start_pos = strpos($str, "value=\"");
        $end_pos = strpos($str, "\"", $start_pos+7);
        $sub = substr($str, $start_pos, $end_pos-$start_pos+1);
        return substr($str, 0, $start_pos) . html_entity_decode($sub) . substr($str, $end_pos+1);

    }
    
    function format_price($price, $separated_by_space = false)
    {
        //echo __FILE__ . ',' . __LINE__ .  ',' . __FUNCTION__;
        if ($separated_by_space)
        {
        	return number_format($price, 2, '.', ' ');
        }
        else 
        {
        	return number_format($price, 2);
        }
    }
    
    function format_percent_discount($price, $separated_by_space = false)
    {
        //echo __FILE__ . ',' . __LINE__ .  ',' . __FUNCTION__;
        if ($separated_by_space)
        {
        	return number_format($price,3, '.', ' ');
        }
        else 
        {
        	return number_format($price,3);
        }
    }
   
     function format_percent_discount_useful_dec($price, $separated_by_space = false)
    {
    	return floatval($this->format_percent_discount($price, $separated_by_space));
    }
    
    
    
    function format_number($number,$separate = ' ')
    {
        return number_format($number, 0, ',', $separate);

    }
    function optionInHurry($date)
    {
        $fulldate = explode(" ", $date);
        $arr = explode("-", $fulldate[0]);
        $date = $arr[1] . '/' . $arr[2] . '/' . $arr[0];
        return (date("d", strtotime($date) -time()) <= HURRY_TIMESTAMP);

    }
    function fullDay($date)
    {
        $time = strtotime($date);
        global $dayText;
        global $moisText;
        return $dayText[date("N", $time) ] . " " . date("d", $time) . " " . $moisText[intval(date("m", $time)) ] . " " . date("Y", $time);

    }
    function redirect($url)
    {
        echo "<meta http-equiv=\"Refresh\" content=\"0; " . APPLICATION_URL . $url . "\">";

    }
    /*function getNameOfDOStatus($status)
    {
        $status_names = array(
            NOT_SENT => _TIP_DO_STATUS_NOT_SENT,
            SENT => _TIP_DO_STATUS_SENT_ANNONCEUR,
            ACCEPTED => _TIP_ACCEPTER_DO,
            MODIFIED => _TIP_ACCEPTER_MODIFIED_DO,
            MODIFIED_ACCEPTED => _TIP_DO_STATUS_MODIFIED_ACCEPTED,
            REFUSED => _TIP_REFUSER_DO
        );
        return $status_names[$status];

    }*/
    function getIconOfDOStatus($status)
    {
        $status_icons = array(
            NOT_SENT => 'fax',
            SENT => 'fax_done',
            MODIFIED => 'fax_about',
            MODIFIED_ACCEPTED => 'fax_done_about'
        );
        return $status_icons[$status];

    }
    function buildImagePath($image_id)
    {
        /*		$images = array(
        IMAGE_NAME_EDIT => 'edit.png',
        IMAGE_NAME_DELETE => 'delete.png',
        IMAGE_NAME_VIEW => 'view.png'
        );
        */
        return APPLICATION_URL . '/images/' . $image_id;

    }
    function imageHTML($image_name, $alt = "")
    {
        return '<img src="' . $this->buildImagePath($image_name) . '" alt="' . $alt . '" />';

    }
    function round($value, $decimal_digit_number = 0)
    {
		
        return round($value,$decimal_digit_number);

    }

    function intval($value)
    {
    	return intval($value);
    }

    function DayName($date)
    {
        $daynames = array(
            0 => _LB_MONDAY,
            1 => _LB_TUESDAY,
            2 => _LB_WEDNESDAY,
            3 => _LB_THURSDAY,
            4 => _LB_FRIDAY,
            5 => _LB_SATURDAY,
            6 => _LB_SUNDAY,
            7 => _LB_DAYOFF
        );
        $day_of_week = date('N', $date);
        return $daynames[$day_of_week-1];

    }

	function encode($str) {
		return utf8_encode($str);
	}

	function formatURL($str, $leng = 0) {
		$arr = array();
		if ($leng && strlen($str) > $leng)
		{
			for ($i=0;$i<=strlen($str)/$leng ;$i++ )
				$arr[]= substr($str, $i*$leng, $leng);
			$url = '';
			foreach ($arr as $v)
				$url .= htmlspecialchars($v) ."<br />";

			return substr($url,0,strlen($url)-6);

		}
		else
			return substr(htmlspecialchars($str), 0, $leng);
	}

	function reduceText($str,$len)
	{
		if (strlen($str)<=$len+3)
		{
			$slen = strlen($str);
			for ($i=1;$i<=$len-$slen+3;$i++)
				$str.=" ";
			return self::text($str);
		}
		return self::text(substr($str,0,$len).'...');
	}

	function changeBreakLine($str)
	{
		$str = $this->htmlencode($str);
		$str = str_replace("\r\n", "<br>", $str);
		$str = str_replace("'", "\'", $str);
		return $this->htmlencode($str);
	}

	function truncate($str, $leng) {
		return substr($str, 0, $leng);
	}

	function format_CPM($price) {
		if ($price == null || $price == "")
			$price = 0;
		return number_format($price, 2);
	}


	function createImageButton($name, $image_name, $type="button", $attrs="", $mouseover=true)
	{
		$button = "";
		if ($name == null || $name == "")
		{
			$name = "imgButton";
		}

		if ($type == "button")
		{
			$button = "<img id=\"$name\" class=\"button\" $attrs alt=\"\" ";
		}
		else
		{
			$button = "<input type=\"image\" name=\"$name\" id=\"$name\" class=\"button\" $attrs ";
		}

		$button .=	" src=\"" . APPLICATION_URL . "/images/" . $image_name . ".png" . "\"";
		if($mouseover==true)
		{
		$button .=	" onmouseover=\"this.src='" . APPLICATION_URL . "/images/" . $image_name . "_hover.png" . "'\"" .
					" onmouseout=\"this.src='" . APPLICATION_URL . "/images/" . $image_name . ".png" . "'\" />";
		}
		else
		{
		$button .=	" />";
		}
		return $button;
	}

	function getHalfHour($h)
	{
		return intval($h/2).'h'.(($h%2)?"30":"00").' - '.intval(($h+1)/2).'h'.((($h+1)%2)?"30":"00");

	}

	function percent($x,$y)
	{
		$x = intval($x);
		$y = intval($y);

		if ($y==0)
			return "";
		else
  			return number_format(intval($x)*100/intval($y), 2)." %";
	}
    
	function date_sql_to_date($date_convert) {
			//$date_convert = "yyyy-mm-dd";
			$pos = strpos($date_convert, '-');
			if ($pos === false)
					return $date_convert;
	
			list($year, $month, $day) = explode("-", $date_convert);
			return $day . "/" . $month . "/" . $year;
	}
    
	/**
	 * @FIXME this is for calling in template, where to put this? 
	 */
	function implodeArr($glue, $arr)
	{
		return implode($glue,$arr);
	}
	
	
	static function displayResizeImage($img_url, $image_file, $max_width, $max_height, $path, $addslases = 0)
	{
		if($image_file && file_exists($path."/".$image_file))
		{
			$img_info = getimagesize($path."/".$image_file); 
			$width = $img_info[0];
			$height = $img_info[1];
			
			$width_ratio = $max_width/$width;
			$height_ratio = $max_height/$height;
			
			$ratio = ($width_ratio < $height_ratio) ? $width_ratio : $height_ratio;
			$ratio = ($ratio < 1) ? $ratio : 1;
			if($addslases != 0)
			{
				echo addslashes("<img src='$img_url/$image_file' width='".intval($width*$ratio)."' height='".intval($height*$ratio)."' style='padding-top:2px'/>");
			}
			else 
			{
				echo "<img src='$img_url/$image_file' width='".intval($width*$ratio)."' height='".intval($height*$ratio)."' style='padding-top:2px'/>";
			}
		}			
		
	}	 
	
	
}
	
	
?>
